<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function getRandom() {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            const num = parseInt(Math.random() * 101);
            if (num > 0 && num <= 50) {
              resolve("成功" + num);
            } else if (num > 50 && num <= 100) {
              reject("失败" + num);
            }
          }, 1000);
        });
      }

      const p = getRandom();
      p.then(
        (res) => {
          console.log(res);
        },
        (err) => {
          console.log(err);
        }
      ).catch((err) => {
        console.log(err);
      });
    </script>
  </body>
</html>
